package com.cbgolf.oa.views;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.cbgolf.oa.R;
import com.cbgolf.oa.adapter.recycle.CommonAdapter;
import com.cbgolf.oa.adapter.recycle.ViewHolder;
import com.cbgolf.oa.base.BaseViewNew;
import com.cbgolf.oa.contract.ICloseParkListContract;
import com.cbgolf.oa.entity.Beans;
import com.cbgolf.oa.event.Events;
import com.cbgolf.oa.manager.DataManager;
import com.cbgolf.oa.util.AnimaUtil;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.DensityUtil;
import com.cbgolf.oa.util.EnumUtil;
import com.cbgolf.oa.util.ErrorUtil;
import com.cbgolf.oa.util.ScreenUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;
import com.cbgolf.oa.widget.TopView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lq
 */
public class CloseParkViewImp extends BaseViewNew implements ICloseParkListContract.View,
    OnRefreshLoadmoreListener {

  @BindView(R.id.topView)
  TopView topView;
  @BindView(R.id.refresh)
  SmartRefreshLayout refreshLayout;
  @BindView(R.id.recycleView)
  RecyclerView recyclerView;

  private CommonAdapter<Beans> adapter;
  private List<Beans> listData = new ArrayList<>();

  private ICloseParkListContract.Waiter worker;

  public CloseParkViewImp(Activity act, ICloseParkListContract.Waiter waiter) {
    ButterKnife.bind(this, act);
    this.worker = waiter;
    super.init(act);
  }

  @Override
  protected void setViews() {
    topView.setTitle(worker.getActivityTitle());
    topView.setTopRightTvVisible(true);
    topView.setRightTvText(context.getString(R.string.add));
    ViewUtils.setRefreshCircleHeader(context, refreshLayout);
    setListAdapter();
  }

  @Override
  protected void setListener() {
    refreshLayout.setOnRefreshLoadmoreListener(this);
    topView.setBackListener(v -> worker.back());
    topView.setTopRightTvListener(v -> {
      worker.addClosePark();
    });
  }

  private void setListAdapter() {
    adapter = new CommonAdapter<Beans>(context, R.layout.item_rcy_close_park, listData) {
      @Override
      protected void convert(ViewHolder holder, Beans bean, int position) {
        if (null == bean) {
          return;
        }
        if (bean.isNoData && listData.size() == 1) {
          holder.setVisible(R.id.layout, false)
              .setVisible(R.id.g_error_ll, true)
              .setImageResource(R.id.g_error_logo_iv, ErrorView.getLogoRes(ErrorUtil.NODATA_CODE))
              .setText(R.id.g_error_tv, TextUtil.Text(bean.errorMsg));
          ViewUtils.setLayoutParams(holder.getView(R.id.g_error_layout_ll), ScreenUtil.getWidth(),
              ScreenUtil.getHeight() - ViewUtils.getStatusBarHeight(context)
                  - DensityUtil.dip2px(context, 48));
          return;
        }
        holder.setVisible(R.id.layout, true)
            .setVisible(R.id.g_error_ll, false)
            .setText(R.id.tv_item_closepark_name, EnumUtil.getCloseParkCause(bean.reason))
            .setText(R.id.tv_item_closepark_starttime, new StringBuilder()
                .append("开始：")
                .append(DateUtil.stampToDate(bean.startTime, DateUtil.formatYMDHm))
                .toString())
            .setText(R.id.tv_item_closepark_endtime, new StringBuilder()
                .append("结束：")
                .append(DateUtil.stampToDate(bean.endTime, DateUtil.formatYMDHm))
                .toString())
            .setText(R.id.tv_item_closepark_state, bean.closured ? "有效" : "无效")
            .setTextColor(R.id.tv_item_closepark_state,
                ContextCompat
                    .getColor(context, bean.closured ? R.color.text_blue2 : R.color.text_gray9))
            .setOnClickListener(R.id.layout, v -> worker.showDetails(bean))
        ;
      }
    };
    LinearLayoutManager lm = new LinearLayoutManager(context);
    lm.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(lm);
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(adapter);
  }

  @Override
  public void showProgress() {
    if (!refreshLayout.isLoading() && !refreshLayout.isRefreshing()) {
      AnimaUtil.showLoading(context);
    }
  }

  @Override
  public void stopProgress() {
    if (null != refreshLayout) {
      refreshLayout.finishRefresh();
      refreshLayout.finishLoadmore();
    }
    AnimaUtil.stopLoading();

  }

  @Override
  public void requestFail(String msg, int code) {
    listData.clear();
    Beans beans = new Beans();
    beans.isNoData = true;
    beans.errorCode = code;
    beans.errorMsg = Util.isNull(msg) ? ErrorUtil.NODATA : msg;
    listData.add(beans);
    adapter.notifyDataSetChanged();
  }

  @Override
  public void requestOver(Events event) {
    if (null == event) {
      return;
    }
    switch (event.msgTarget) {
      case DataManager.CLOSE_PARK_LIST:
        stopProgress();
        showResult(event);
        break;
      default:
        break;
    }

  }

  private void showResult(Events event) {
    if (Util.listIsNull(event.listData)) {
      if (Util.listIsNull(listData)) {
        requestFail(event.errorMsg, event.errorCode);
      }
      return;
    }
    listData.clear();
    listData.addAll(event.listData);
    adapter.notifyDataSetChanged();
  }

  @Override
  public void onLoadmore(RefreshLayout refreshlayout) {
    worker.loadMore();
  }

  @Override
  public void onRefresh(RefreshLayout refreshlayout) {
    worker.refresh();
  }
}
